package com.example.silence.mypainter.All.Activity;

import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.silence.mypainter.R;

import deadline.statebutton.StateButton;

public class MainActivity extends AppCompatActivity {

    private Animation a1, a2, a3, a4, a5, a6, a7, a8, a9, a10;
    private TextView dTx, titleTv, verTv;
    private ImageView tP1, tP2, tF1, tF2;
    private StateButton bSbt;
    private EditText et1, et2;

    private long startTime = 0;

    @Override
    public void onBackPressed() {

        long currentTime = System.currentTimeMillis();
        if ((currentTime - startTime) >= 2000) {
            Toast.makeText(MainActivity.this, "再按一次退出", Toast.LENGTH_SHORT).show();
            startTime = currentTime;
        } else {
            finish();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final DisplayMetrics dm = getResources().getDisplayMetrics();

        dTx = findViewById(R.id.designer);//软件设计者姓名
        tP1 = findViewById(R.id.t1_picture);//标题上层图片
        tP2 = findViewById(R.id.t2_picture);//标题下层图片
        tF1 = findViewById(R.id.f1);//左下角三角形
        tF2 = findViewById(R.id.f2);//右下角三角形
        bSbt = findViewById(R.id.start_draw);//开始绘图按钮
        titleTv = findViewById(R.id.m_title);//标题
        et1 = findViewById(R.id.width_s);//宽
        et2 = findViewById(R.id.height_s);//高
        verTv = findViewById(R.id.version);//最下方的版本号

        et1.setHint("画布宽度(<=" + dm.widthPixels + ")");
        et2.setHint("画布高度(<=" + dm.heightPixels + ")");

        a1 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.designer_m);//设计者出现的动画
        a2 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.tp1);//标题上层图片移动
        a3 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.tp2);//标题下层图片移动
        a4 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.tf1);//左下角三角形移动
        a5 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.tf2);//右下角三角形移动
        a6 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.to_top);//向上消失效果
        a7 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.to_bottom);//向下消失效果
        a8 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.to_left);
        a9 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.to_right);
        a10 = AnimationUtils.loadAnimation(MainActivity.this, R.anim.to_center);

        dTx.startAnimation(a1);
        tP1.startAnimation(a2);
        tP2.startAnimation(a3);
        tF1.startAnimation(a4);
        tF2.startAnimation(a5);

        bSbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(et1.getText()) || TextUtils.isEmpty(et2.getText())) {
                    Toast.makeText(MainActivity.this, "使用默认宽高", Toast.LENGTH_SHORT).show();
                    goToDraw(dm.widthPixels - 100, dm.heightPixels - 500);
                } else if (Integer.parseInt(et1.getText().toString()) == 0 || Integer.parseInt(et2.getText().toString()) == 0) {
                    Toast.makeText(MainActivity.this, "属性值不能为0", Toast.LENGTH_SHORT).show();
                } else if (Integer.parseInt(et1.getText().toString()) > dm.widthPixels || Integer.parseInt(et2.getText().toString()) > dm.heightPixels) {
                    Toast.makeText(MainActivity.this, "请重新输入", Toast.LENGTH_SHORT).show();
                } else {
                    goToDraw(Integer.parseInt(et1.getText().toString()), Integer.parseInt(et2.getText().toString()));
                }

            }
        });
    }

    protected void goToDraw(final int w, final int h) {

        titleTv.startAnimation(a6);
        dTx.startAnimation(a6);
        tP1.startAnimation(a6);
        tP2.startAnimation(a6);
        verTv.startAnimation(a7);
        bSbt.startAnimation(a7);
        et1.startAnimation(a10);
        et2.startAnimation(a10);
        tF1.startAnimation(a8);
        tF2.startAnimation(a9);

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this, DrawActivity.class);
                intent.putExtra("width", w);
                intent.putExtra("height", h);
                startActivity(intent);
                overridePendingTransition(R.anim.into_draw, R.anim.out_draw);
                finish();
            }
        }, 200);

    }
}
